package com.sk.weichat.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sk.weichat.AppConstant;
import com.sk.weichat.R;
import com.sk.weichat.bean.AttentionUser;
import com.sk.weichat.bean.Friend;
import com.sk.weichat.helper.AvatarHelper;
import com.sk.weichat.sortlist.BaseSortModel;
import com.sk.weichat.ui.me.PersonalCenterActivity;
import com.sk.weichat.util.TimeUtils;
import com.sk.weichat.util.ViewHolder;
import com.swipemenulib.SwipeMenuLayout;

import java.util.List;

public class BlackAdapter extends BaseQuickAdapter<AttentionUser, BaseViewHolder> {

    private removeBlackFriendListener mListener;
    public BlackAdapter(@Nullable List<AttentionUser> data,removeBlackFriendListener mListener) {
        super(R.layout.item_black_friend, data);
        this.mListener = mListener;
    }

    @Override
    protected void convert(BaseViewHolder helper, AttentionUser item) {
        SwipeMenuLayout swipemenulayout = helper.getView(R.id.swipemenulayout);
        ImageView avatar_img = helper.getView(R.id.avatar_img);
        TextView nick_name_tv = helper.getView( R.id.nick_name_tv);
        TextView tvTime = helper.getView(R.id.tv_call_msg_time);
        TextView delete_tv  =helper.getView(R.id.delete_tv);
        RelativeLayout layoutItem=helper.getView(R.id.layout_black_friend_item);

        // 设置头像
        AvatarHelper.getInstance().displayAvatar("", item.getUserId(), avatar_img, false);
        // 昵称
        nick_name_tv.setText(item.getNickname());

        String  time = TimeUtils.getFriendlyTimeDesc(mContext,item.getFriends().getAddBlackTime()==0?item.getFriends().getTimeSend():item.getFriends().getAddBlackTime());
        //发送时间
        if (TextUtils.isEmpty(time)){
            tvTime.setVisibility(View.GONE);
        }else {
            tvTime.setVisibility(View.VISIBLE);
            tvTime.setText("拉黑时间："+time);
        }

        delete_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.removeBlackFriend(helper.getAdapterPosition());
                swipemenulayout.quickClose();
            }
        });

        //item点击事件
        layoutItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { // 头像的点击事件
                Intent intent = new Intent(mContext, PersonalCenterActivity.class);
                intent.putExtra(AppConstant.EXTRA_USER_ID, item.getUserId());
                mContext.startActivity(intent);

            }
        });
        //头像点击事件
        avatar_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { // 头像的点击事件
                Intent intent = new Intent(mContext, PersonalCenterActivity.class);
                intent.putExtra(AppConstant.EXTRA_USER_ID, item.getUserId());
                mContext.startActivity(intent);

            }
        });
    }
    public interface removeBlackFriendListener {

        void removeBlackFriend(int position);// 移除单条消息

    }
//    private Context mContext;
//    private List<AttentionUser> mSortFriends;
//    private removeBlackFriendListener mListener;
//
//    public BlackAdapter(Context context, List<AttentionUser> sortFriends, removeBlackFriendListener mListener) {
//        mContext = context;
//        mSortFriends = sortFriends;
//        this.mListener = mListener;
//
//
//    }
//    public void setData(List<AttentionUser> sortFriends) {
//        mSortFriends = sortFriends;
//        notifyDataSetChanged();
//    }
//    @Override
//    public int getCount() {
//        return mSortFriends.size();
//    }
//
//    @Override
//    public Object getItem(int position) {
//        return position;
//    }
//
//    @Override
//    public long getItemId(int position) {
//        return position;
//    }
//
//    @Override
//    public View getView(int position, View convertView, ViewGroup parent) {
//
//        if (convertView == null) {
//            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_black_friend, parent, false);
//        }
//        AttentionUser attentionUser = mSortFriends.get(position);
//        SwipeMenuLayout swipemenulayout = ViewHolder.get(convertView,R.id.swipemenulayout);
//        ImageView avatar_img = ViewHolder.get(convertView, R.id.avatar_img);
//        TextView nick_name_tv = ViewHolder.get(convertView, R.id.nick_name_tv);
//        TextView tvTime = ViewHolder.get(convertView, R.id.tv_call_msg_time);
//        TextView delete_tv  = ViewHolder.get(convertView,R.id.delete_tv);
//        RelativeLayout layoutItem=ViewHolder.get(convertView, R.id.layout_black_friend_item);
//
//        // 设置头像
//        AvatarHelper.getInstance().displayAvatar("", attentionUser.getUserId(), avatar_img, false);
//
////        AvatarHelper.getInstance().displayAvatar(attentionUser.getUserId(), avatar_img, true);
//        // 昵称
//        nick_name_tv.setText(attentionUser.getNickname());
//        //发送时间
//        tvTime.setText("拉黑时间："+TimeUtils.getFriendlyTimeDesc(mContext,attentionUser.getFriends().getAddBlackTime()==0?attentionUser.getFriends().getTimeSend():attentionUser.getFriends().getAddBlackTime()));
//        delete_tv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mListener.removeBlackFriend(position);
//                swipemenulayout.quickClose();
//            }
//        });
//
//        //item点击事件
//        layoutItem.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) { // 头像的点击事件
//                Intent intent = new Intent(mContext, PersonalCenterActivity.class);
//                intent.putExtra(AppConstant.EXTRA_USER_ID, attentionUser.getUserId());
//                mContext.startActivity(intent);
//
//            }
//        });
//        //头像点击事件
//        avatar_img.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) { // 头像的点击事件
//                Intent intent = new Intent(mContext, PersonalCenterActivity.class);
//                intent.putExtra(AppConstant.EXTRA_USER_ID, attentionUser.getUserId());
//                mContext.startActivity(intent);
//
//            }
//        });
//        return convertView;
//    }
//
//    public interface removeBlackFriendListener {
//
//        void removeBlackFriend(int position);// 移除单条消息
//
//    }
}
